//出厂测试

#include "APP_Main.h"
#include "../Codes/LCD/HT162X_Parts_Num.h"
#include "../MCU_Mold/MCU_AbstractTube.h"
#include "FactoryTest.h"


uint8_t FactoryEnable = 0;


void Factory_Check(uint16_t checkTarget)
{
    uint8_t i;

    if(ReadIO_Key3 == 0)
    {
        for (i = 0; i < 30; i++) keyboard_delay15ms();
        if(ReadIO_Key3 == 0)
        {
            FactoryEnable = 1;
            UI_System.UI_Show = UI_ON;
            IgniterClose;
            HeaterFanClose;
            ExhaustFanClose;
            // FanCtrl_Fan = 1;
            // FanCtrl_Heater = 1;
            // FanCtrl_Feed = 1;
            // while(checkTarget == 1)
            // {
            //     KeyDetection();
            //     SwitchKey();
            //     Lcd_Factory_Show(Switch_State);
            //     WDT_Clear();
            // }
        }
    }
}

void Factory_Refresh(void)
{
    uint16_t temp = 0;
    uint32_t param;
    
    if(UI_System.UI_Show == UI_ON)
    {
        KeyLed_ON;
        if(Temp_Param.oven_set != Temp_Shutoff)
        {
            Lcd_Probe1_Show(Char_ON);
            Lcd_Probe2_Show(Char_ON);
            Lcd_Actual(Char_ON);
            Lcd_Timer_Colon(Char_OFF);
 //           Lcd_PSet(Char_ON);
            // Lcd_Numbers(Temp_Param.p_value,NULL,NULL,NULL,PSet_seg1);
            Lcd_Number(Temp_Param.oven_set / 100 % 10,Timer_seg1);
			Lcd_Number(Temp_Param.oven_set / 10 % 10,Timer_seg2);
			Lcd_Number(Temp_Param.oven_set % 10,Timer_seg3);
            Lcd_Number(Tube_Encoder_Space,Timer_seg4);
            Lcd_Number(Tube_Encoder_Space,Timer_seg5);
            Temp_Param.oven_Err = Pt_Normal;
            param = Temp_Param.oven;
            if(UI_System.Grill_Unit == Unit_C)
            {
                if(RTD[0].status == Pt_Normal) param = F_Convert_C(param);
            }
            Lcd_Numbers(param,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);

            param = Temp_Param.probe1;
            if(UI_System.Grill_Unit == Unit_C)
            {
                if(RTD[1].status == Pt_Normal) param = F_Convert_C(param);
            }
            Lcd_Numbers(param,NULL,Probe1_seg1,Probe1_seg2,Probe1_seg3);
            Temp_Param.probe1_Err = Pt_Normal;

            param = Temp_Param.probe2;
            if(UI_System.Grill_Unit == Unit_C)
            {
                if(RTD[2].status == Pt_Normal) param = F_Convert_C(param);
            }
            Lcd_Numbers(param,NULL,Probe2_seg1,Probe2_seg2,Probe2_seg3);
            Temp_Param.probe2_Err = Pt_Normal;
            // Lcd_Number(Tube_Encoder_Space,Probe2_seg1);
            // Lcd_Number(Tube_Encoder_Space,Probe2_seg2);
            // Lcd_Number(Tube_Encoder_Space,Probe2_seg3);

            Lcd_Cook(Char_ON);
            Lcd_Backlight_ON;
        }
        else
        {
            temp = 1;
        }
    }
    else
    {
        KeyLed_OFF;
        temp = 1;
    }
    if(temp == 1)
    {
        Lcd_Backlight_OFF;
    }
}

void Factory_Show(uint16_t temp_set)
{
    static uint16_t Factory_Num = 0;
    uint16_t i;

    if(ReadIO_Key1 == 0)
    {
        for (i = 0; i < 10; i++) keyboard_delay15ms();
        if(ReadIO_Key1 == 0)
        {
            IgniterOpen;
            HeaterFanClose;
            ExhaustFanClose;
        }
    }
    if(ReadIO_Key2 == 0)
    {
        for (i = 0; i < 10; i++) keyboard_delay15ms();
        if(ReadIO_Key2 == 0)
        {
            IgniterClose;
            HeaterFanOpen;
            ExhaustFanClose;
        }
    }
    if(ReadIO_Key3 == 0)
    {
        for (i = 0; i < 10; i++) keyboard_delay15ms();
        if(ReadIO_Key3 == 0)
        {
            IgniterClose;
            HeaterFanClose;
            ExhaustFanOpen;
        }
    }

    if((temp_set == Temp_Set_Smoke) && (Factory_Num != 0))
    {
        Factory_Num = 0;
    }
    else if((temp_set == Temp_Set_200) && (Factory_Num != 111))
    {
        Factory_Num = 111;
    }
    else if((temp_set == Temp_Set_225) && (Factory_Num != 222))
    {
        Factory_Num = 222;
    }
    else if(temp_set == Temp_Set_250) Factory_Num = 333;
    else if(temp_set == Temp_Set_300) Factory_Num = 444;
    else if(temp_set == Temp_Set_350) Factory_Num = 555;
    else if(temp_set == Temp_Set_400) Factory_Num = 666;
    else if(temp_set == Temp_Set_450) Factory_Num = 777;
    else if(temp_set == Temp_Set_475) Factory_Num = 888;
    else if(temp_set == Temp_Set_500) Factory_Num = 999;

    UI_System.Timer1.Actual = 0;
    Temp_Param.oven_set = Factory_Num;
    Temp_Param.p_value = Factory_Num % 10;
    Temp_Param.oven_Err = RTD[0].status;
    Temp_Param.probe1_Err = RTD[1].status;
    Temp_Param.probe2_Err = RTD[2].status;
    if(RTD[0].status != Pt_Normal)
    {
        Temp_Param.oven = Factory_Num;
        Temp_Param.oven_Err = Pt_Normal;
        Factory_Lcd_F(0,Char_ON);
        Factory_Lcd_C(0,Char_ON);
    }
    else
    {
        Temp_Param.oven = RTD[0].value;
        if(UI_System.Grill_Unit == Unit_F)
        {
            Factory_Lcd_F(0,Char_ON);
            Factory_Lcd_C(0,Char_OFF);
        }
        else if(UI_System.Grill_Unit == Unit_C)
        {
            Factory_Lcd_F(0,Char_OFF);
            Factory_Lcd_C(0,Char_ON);
        }
    }
    if(RTD[1].status != Pt_Normal)
    {
        Temp_Param.probe1 = Factory_Num;
        Temp_Param.probe1_Err = Pt_Normal;
        Factory_Lcd_F(1,Char_ON);
        Factory_Lcd_C(1,Char_ON);
    }
    else
    {
        Temp_Param.probe1 = RTD[1].value;
        if(UI_System.Grill_Unit == Unit_F)
        {
            Factory_Lcd_F(1,Char_ON);
            Factory_Lcd_C(1,Char_OFF);
        }
        else if(UI_System.Grill_Unit == Unit_C)
        {
            Factory_Lcd_F(1,Char_OFF);
            Factory_Lcd_C(1,Char_ON);
        }
    }
    if(RTD[2].status != Pt_Normal)
    {
        Temp_Param.probe2 = Factory_Num;
        Temp_Param.probe2_Err = Pt_Normal;
        Factory_Lcd_F(2,Char_ON);
        Factory_Lcd_C(2,Char_ON);
    }
    else
    {
        Temp_Param.probe2 = RTD[2].value;
        if(UI_System.Grill_Unit == Unit_F)
        {
            Factory_Lcd_F(2,Char_ON);
            Factory_Lcd_C(2,Char_OFF);
        }
        else if(UI_System.Grill_Unit == Unit_C)
        {
            Factory_Lcd_F(2,Char_OFF);
            Factory_Lcd_C(2,Char_ON);
        }
    }
    Factory_Refresh();
    HT162X_RAM_Tx();
}

void Factory_main(void)
{
    uint16_t temp;
    uint32_t FanCtrl_Fan = 0;
    uint32_t FanCtrl_Heater = 1;
    uint32_t FanCtrl_Feed = 1;

    KeyDetection();
    Factory_Check(key_param.once3);
    while (FactoryEnable)
    {
        ADSampling();
        KeyDetection();
        SwitchKey();
        Factory_Show(Switch_State);
        WDT_Clear();
    }
}
